/* *********************************************
as.ParticleScreen
************************************************

Description:	Screen full of particles

Creation date:	June 8, 2012
Created by:	Kirby Szeto (Plus K Studios)
Modified:	-
Notes:	-

***********************************************/

import flash.display.*;
import flash.filters.BlurFilter;

class as.ParticleScreen extends MovieClip {
	// class definition
	
	
	// variables
	var particles:Array;
	var inc:Number;
	var continuous:Boolean = false;
	
	public function ParticleScreen () {
		particles = [];
		
		for (var e in this) {
			if (typeof this[e] == "movieclip") {
				var p = this[e];
				p._ty = p._y;
				p._rotation = Math.random() * 360;
				if ( particles.length % 2 == 0 ) {
					p._xscale *= -1;
				}
				p._tr = -3 + Math.random() * 6;
				p._yvel = 0;
				p._y = -50 + p._xscale/10;
				
				particles.push( p );
			}
		}
		
		particles.sort( order );
		
		inc = 0;
		//dropInAll();
	}
	
	function order(b, a):Number { 
		var name1:Number = a._ty; 
		var name2:Number = b._ty; 
		if (name1<name2) { 
		return -1; 
		} else if (name1>name2) { 
		return 1; 
		} else { 
		return 0; 
		} 
	} 
	
	public function startDrop() {
		continuous = false;
		onEnterFrame = updateDropIn;
	}
	public function dropSomeMore( multiple:Boolean ) {
		var n = multiple ? Math.ceil( Math.random() * 2 ) : 1;
		inc += n;
		inc = Math.min( inc, particles.length );
	}
	public function dropInAll() {
		
		continuous = true;
		onEnterFrame = updateDropIn;
		
	}
	private function updateDropIn() {
		if (continuous) dropSomeMore( true );
		for (var i = 0; i < inc; i++) {
			var p = particles[i];
		
			var bf:BlurFilter = new BlurFilter( 0, 0 );
			
			if (p._y < p._ty) {
				bf.blurY = p._yvel/2;
				p._y += p._yvel;
				p._yvel += 3;
				p._rotation += p._tr;
			} 
			p.filters = [ bf ];
			p._y = Math.min( p._y, p._ty );
		}
	}
	
	public function dropOut() {
		inc = 0;
		for (var i = 0; i < particles.length; i++) {
			var p = particles[i];
			p._yvel = 0;
		}
		onEnterFrame = updateDropOut;
	}
	private function updateDropOut() {
		var n = inc;
		inc += 2;
		n = Math.min( n, particles.length );
		for (var i = 0; i < n; i++) {
			var p = particles[i];
		
			if (p._y < 800) {
				var bf:BlurFilter = new BlurFilter( 0, p._yvel/2 );
				p._y += p._yvel;
				p._yvel += 3;
				p._rotation += p._tr;
				p.filters = [ bf ];
			} 
		}
	}
	
}